package com.nhim.cms.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;


/**
 * ContentType entity. @author Huy Lieu Ta
 */
@Entity
@Table(name = "nhim_content_type", catalog = "nhim_cms")
public class ContentType implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Fields

	private Integer id;
	private String description;
	private String name;
	private Integer version;
	private Set<Group> groups = new HashSet<Group>(0);
	private Set<ContentTypeElementDefinition> contentTypeElementDefinitions = new HashSet<ContentTypeElementDefinition>(
			0);

	// Constructors

	/** default constructor */
	public ContentType() {
	}

	/** minimal constructor */
	public ContentType(String name) {
		this.name = name;
	}

	/** full constructor */
	public ContentType(String description, String name,
			Set<Group> groups,
			Set<ContentTypeElementDefinition> contentTypeElementDefinitions) {
		this.description = description;
		this.name = name;
		this.groups = groups;
		this.contentTypeElementDefinitions = contentTypeElementDefinitions;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(name = "name", nullable = false, length = 100)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 200)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@Version
	@Column(name = "version")
	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH}, fetch = FetchType.LAZY)
	@JoinTable(name = "nhim_content_type_group", catalog = "nhim_cms", joinColumns = { @JoinColumn(name = "content_type_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) })
	public Set<Group> getGroups() {
		return this.groups;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}
	
	public void addGroup(Group group) {  
	     if (!getGroups().contains(group)) {   
	             getGroups().add(group);  
	     }  
	} 
	
	public void removeGroup(Group group) {  
	     if (getGroups().contains(group)) {   
	             getGroups().remove(group);  
	     }  
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "contentType")
	public Set<ContentTypeElementDefinition> getContentTypeElementDefinitions() {
		return this.contentTypeElementDefinitions;
	}

	public void setContentTypeElementDefinitions(
			Set<ContentTypeElementDefinition> contentTypeElementDefinitions) {
		this.contentTypeElementDefinitions = contentTypeElementDefinitions;
	}

}